/**
 * 领取会员卡界面
 * author:zhangjlt
 * date:2017年3月16日13:30:42
 */
;
define(function(require, exports, module) {
	var wechat = require("wechat");
	var wx = require("lib/jweixin-1.2.0");
	var collectCard = {
		data: {},
		/**
		 * bindEvent 页面事件
		 */
		bindEvent: function() {
			$("#btn_collectCard").unbind().on("click", function() {
				collectCard.collect();
			});
		},
		/**
		 * 进行页面路由监听配置
		 * 
		 */
		bindRout: function() {
			//公共页面监听路由
			Path.map("#/index/:page_name").to(function() {
				console.log(this.params["page_name"]);
				var page_name = this.params["page_name"];
				var nextPageFrom = "right"; //从右边过来
				//页面加载方向
				if(page_name == "nearShop" || page_name == "home" || page_name == "my") {
					nextPageFrom = "none";
				}
				//记录状态
				common.loadPage({
					pageUrl: "html/" + page_name + ".html",
					reload: true,
					from: nextPageFrom
				});
			});
			//公共页面监听路由
			Path.map("#home").to(function() {
				//返回首页
				common.loadPage({
					pageUrl: "html/index.html",
					reload: true,
					from: "none"
				});
			});
			Path.listen();
		},
		
		/**
		 * 页面数据加载
		 */
		initData: function() {
			var url = location.href.split('#')[0];
			var urlstr = encodeURIComponent(url);
			var appid = window.localStorage.getItem("appId");
			var param = {

			};
			common.loadData({
				url: "/jssdk/getjssdksign?url=" + urlstr + "&appid=" + appid, //"/UMPPubServlet?method=getWxUserInfo",
				param: param,
				successCallBack: function(res) {
					var data = JSON.parse(res.data);
					
					wx.config({
						debug: false, // 开启调试模式,调用的所有api的返回值会在客户端alert出来，若要查看传入的参数，可以在pc端打开，参数信息会通过log打出，仅在pc端时才会打印。
						appId: window.localStorage.getItem("appId"), // 必填，公众号的唯一标识
						timestamp: data.timestamp, // 必填，生成签名的时间戳
						nonceStr: data.nonceStr, // 必填，生成签名的随机串
						signature: data.signature, // 必填，签名，见附录1
						jsApiList: [
							'onMenuShareTimeline',
							'onMenuShareAppMessage',
							'onMenuShareQQ',
							'chooseImage',
							'scanQRCode',
							'uploadImage',
							'chooseWXPay',
							'addCard',
							'closeWindow'
						] // 必填，需要使用的JS接口列表，所有JS接口列表见附录2
					});
					wx.ready(function() {
						collectCard.initCardSign();
					});
					wx.error(function(res) {
						//alert(JSON.stringify(res));
					});
				}, //回调函数

				cache: false
			});
		},
		initCardSign: function() {
			var cardid = window.app.card_Id;
			
			var appid = window.localStorage.getItem("appId");
			var openid = window.localStorage.getItem("openId");
			var param = {

			};
			common.loadData({
				url: "/card/getcardsign?appid=" + appid + "&openid=" + openid + "&cardid=" + cardid, //"/UMPPubServlet?method=getWxUserInfo",
				param: param,
				successCallBack: function(res) {
					alert(res.data);
					var data = JSON.parse(res.data);
					collectCard.data = data;
					collectCard.collect();
				}, //回调函数

				cache: false
			});

		},
		collect: function() {
			var cardid = window.app.card_Id;
			var cardExt={
				"openid":window.localStorage.getItem("openId"),
				"timestamp":collectCard.data.timestamp,
				"nonce_str":collectCard.data.nonce_str,
				"signature":collectCard.data.signature
			}
			var cardExtstr = JSON.stringify(cardExt);
			wx.addCard({
				cardList: [{
					cardId: cardid,
					cardExt: cardExtstr
				}], // 需要添加的卡券列表
				success: function(res) {
					//alert(JSON.stringify(res));
					var cardList = res.cardList; // 添加的卡券列表信息
					wx.closeWindow();
					
				}
			});
		},
		/**
		 * each page init mothed
		 */
		initialize: function() {
			this.initData();
			//事件绑定
			this.bindEvent();
			//路由绑定
			this.bindRout();
		}
	};

	module.exports = collectCard;
});